package com.rzico.sbl.ui.report_0537

import androidx.lifecycle.MutableLiveData
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.ManagerContent
import com.rzico.sbl.model.MessageType
import com.rzico.sbl.model.ResponseModel
import com.rzico.sbl.ui.report.other.ReportUrl
import com.rzico.sbl.viewmodel.CommonViewModel
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import io.reactivex.Single

class QFHomeViewModel : CommonViewModel() {

    val mNotice = MutableLiveData("")
    val mMessageEvent = MutableLiveData(0)

    // 财务5
    fun financeIcons() = arrayListOf(
        ManagerContent("订单", R.mipmap.qf_order, "财务"),
        ManagerContent("水站统计", R.mipmap.qf_station, "水站统计")
    )

    // 办公室11
    fun officeIcons() = arrayListOf(
        ManagerContent("订单", R.mipmap.qf_order, "办公室"),
        ManagerContent("水站统计", R.mipmap.qf_station, "水站统计")
    )

    /**
     * 获取公告信息
     */
    fun getNotice() {
        request(
            isLoadingBefore = false,
            isLoadingAfter = false,
            requestUrl = BaseUrl.notice,
            requestMethod = HttpMethod.GET,
            onSuccess = {
                mNotice.value = if (it == "{}") "" else it
            }
        )
    }

    /**
     * 消息数量
     */
    fun messageCount() =
        mDisposables.add(
            OkGo.get<BaseResponse<ResponseModel<MessageType>>>(BaseUrl.msgSum)
                .converter(object : JsonConvert<BaseResponse<ResponseModel<MessageType>>>() {})
                .adapt(FlowableBody())
                .compose(applyFlowableSchedulers())
                .map { it.data.data }
                .subscribeByFinally(
                    event = this,
                    onSuccess = { list ->
                        var totalCount = 0
                        list.forEach { totalCount += it.num.intValue }
                        mMessageEvent.value = totalCount
                    }
                )
        )

    /**
     * 订单数量
     */
    fun orderCount(): Single<String> =
        RxSingleHelper.requestString(
            url = ReportUrl.getBaseUrl().reportCut,
            params = getParams("type" to "1")
        )
            .compose(applySchedulers())

}